package com.lxw.weather.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.lxw.weather.R;
import com.lxw.weather.adapter.HourlyForecastAdapter;
import java.util.ArrayList;
import java.util.List;

public class HourlyForecastFragment extends Fragment {
    private RecyclerView recyclerView;
    private HourlyForecastAdapter adapter;
    private List<com.lxw.weather.model.HourlyForecast> forecastList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hourly_forecast, container, false);
        recyclerView = view.findViewById(R.id.hourlyRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));

        //传递 requireContext()
        adapter = new HourlyForecastAdapter(forecastList, requireContext());
        recyclerView.setAdapter(adapter);
        return view;
    }

    public void updateData(List<com.lxw.weather.model.HourlyForecast> forecasts) {
        if (adapter != null) {
            forecastList.clear();
            forecastList.addAll(forecasts);
            adapter.notifyDataSetChanged();
        }
    }
}